//
//  LGTreatRecordTableCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/11.
//

#import "LGTreatRecordTableCell.h"
#import "LGCheckPhotoTool.h"
@interface LGTreatRecordTableCell()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *statusLabel;
/**   */
@property (nonatomic , strong)UILabel *startTime;
/**   */
@property (nonatomic , strong)UILabel *endTime;
/**   */
@property (nonatomic , strong)UILabel *typeTitle;
/**   */
@property (nonatomic , strong)UILabel *typeLabel;
/**   */
@property (nonatomic , strong)UILabel *photoTitle;
/**   */
@property (nonatomic , strong)UIImageView *photoView;
/**   */
@property (nonatomic , strong)NSMutableArray *itemArray;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , assign)NSInteger count;

@end

@implementation LGTreatRecordTableCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeSingle;
        self.count = isPad?6:3;
        self.photoW = (Screen_W-viewPix(78)-viewPix(10)*(self.count-1))/self.count;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    // 1-手术、 2-放疗、 3-化疗、 4-靶向治疗、 5-免疫治疗、 6-内分泌治疗、 7-中医治疗、 8-介入治疗、 9-其他治疗
    NSString *type = LGNSString(dataDic[@"treat_type"]);
    NSString *title = @"";
    NSString *startTitle = @"开始日期";
    NSString *typeContent = @"";
    if ([type isEqualToString:@"1"]) {
        //手术
        title = @"手术";
        startTitle = @"手术日期";
    }else if ([type isEqualToString:@"2"]){
        //放疗
        title = LGNSString(dataDic[@"radiotherapy_site"]);
        typeContent = LGNSString(dataDic[@"radiotherapy_type"]);
        
    }else if ([type isEqualToString:@"9"]){
        //其他治疗
        title = LGNSString(dataDic[@"treat_title"]);
        
    }else{
        title = LGNSString(dataDic[@"medicine_name"]);
    }
    self.titleLabel.text = title;
    CGFloat timeTop = (title.length>0)?viewPix(10):0;
    [self.startTime mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(timeTop);
    }];
    
    NSString *endTime = LGNSString(dataDic[@"end_date"]);
    self.statusLabel.text = (endTime.length<=6)?@"进行中":@"已结束";
    NSString *statusColor = (endTime.length<=6)?kThemeColor:@"#909399";
    self.statusLabel.textColor = [UIColor colorWithString:statusColor];
    self.startTime.text = [NSString stringWithFormat:@"%@  %@",startTitle,dataDic[@"start_date"]];
    self.endTime.text = [NSString stringWithFormat:@"结束日期  %@",endTime];
    self.endTime.hidden = (endTime.length<=6)?YES:NO;
    
    
    
    //放疗类型
    self.typeTitle.hidden = (typeContent.length>0)?NO:YES;
    self.typeLabel.hidden = (typeContent.length>0)?NO:YES;
    self.typeLabel.text = (typeContent.length>0)?typeContent:@" ";
    CGFloat typeTop = (typeContent.length>0)?viewPix(12):-viewPix(22);
    [self.photoTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.mas_bottom).offset(typeTop);
    }];
    
    self.itemArray = dataDic[@"files"];
    self.photoTitle.text = (self.itemArray.count>0)?@"处方图片":@"";
    self.photoTitle.hidden = (self.itemArray.count>0)?NO:YES;
    self.photoView.hidden = (self.itemArray.count>0)?NO:YES;
    CGFloat photoTop = (self.itemArray.count>0)?viewPix(7):-viewPix(25);
    CGFloat photoHeight = (self.itemArray.count>0)?viewPix(100):viewPix(15);
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoTitle.mas_bottom).offset(photoTop);
        make.height.equalTo(@(photoHeight));
    }];
    if (self.itemArray.count>0) {
        [self.photoView sd_setImageWithURL:[NSURL URLWithString:LGNSString(self.itemArray[0])]];
    }
}

-(void)checkAllPhotos{
    LGCheckPhotoTool *photoView = [[LGCheckPhotoTool alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    photoView.picArray = self.itemArray;
    [[ObjectTool mainWindow] addSubview:photoView];
    [photoView showViewAnimation:CGPointMake(0, 0) index:0];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.statusLabel];
    [self.baseView addSubview:self.startTime];
    [self.baseView addSubview:self.endTime];
    [self.baseView addSubview:self.typeTitle];
    [self.baseView addSubview:self.typeLabel];
    [self.baseView addSubview:self.photoTitle];
    [self.baseView addSubview:self.photoView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(70));
    }];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(13));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
//        make.centerY.equalTo(self.titleLabel);
    }];
    [self.startTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(10));
        make.left.equalTo(self.titleLabel);
    }];
    [self.endTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(180));
        make.centerY.equalTo(self.startTime);
    }];
    [self.typeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.startTime.mas_bottom).offset(viewPix(12));
        make.left.equalTo(self.titleLabel);
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeTitle.mas_right).offset(viewPix(12));
        make.top.equalTo(self.typeTitle).offset(2);
        make.right.equalTo(self.statusLabel);
    }];
    [self.photoTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.typeLabel.mas_bottom).offset(viewPix(14));
        make.left.equalTo(self.titleLabel);
    }];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoTitle.mas_bottom).offset(viewPix(7));
        make.bottom.equalTo(self.baseView).offset(-viewPix(12));
        make.left.equalTo(self.titleLabel);
        make.width.equalTo(@(viewPix(187)));
        make.height.equalTo(@(0));
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)statusLabel{
    if(!_statusLabel){
        _statusLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _statusLabel;
}

-(UILabel *)startTime{
    if(!_startTime){
        _startTime = [UILabel lableWithText:@"" colorString:@"#909399" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _startTime;
}

-(UILabel *)endTime{
    if(!_endTime){
        _endTime = [UILabel lableWithText:@"" colorString:@"#909399" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _endTime;
}

-(UILabel *)typeTitle{
    if(!_typeTitle){
        _typeTitle = [UILabel lableWithText:@"放疗类型" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _typeTitle;
}

-(UILabel *)typeLabel{
    if(!_typeLabel){
        _typeLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _typeLabel;
}

-(UILabel *)photoTitle{
    if(!_photoTitle){
        _photoTitle = [UILabel lableWithText:@"处方单图片" colorString:@"#282828" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _photoTitle;
}

-(UIImageView *)photoView{
    if(!_photoView){
        _photoView = [[UIImageView alloc]init];
        _photoView.userInteractionEnabled = YES;
        [_photoView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkAllPhotos)]];
    }
    return _photoView;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

@end
